//
// Created by 20792 on 24-8-20.
//

#include "send_water_pump.h"
// 封装json
char* json_pack_water_pump(WP* pump){
    cJSON* json = cJSON_CreateObject();
    cJSON_AddNumberToObject(json, "type", pump->type);
    printf("%d\n",pump->type);
    cJSON_AddNumberToObject(json, "pump_type", pump->pump_type);
    printf("%d\n",pump->pump_type);
    cJSON_AddNumberToObject(json, "id", pump->id);
    printf("%d\n",pump->id);
    cJSON_AddStringToObject(json, "pump_name", pump->pump_name);
    printf("%s\n",pump->pump_name);
    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    printf("json成功\n");
    return dest;
}

// 发送水泵信息
int send_water_pump(WP* pump,int sock){
    int ret = 0;
    char *dest = NULL;
    printf("到json前\n");
    dest = json_pack_water_pump(pump);
    puts(dest);
    ret = send(sock, dest, 1024, 0);
    if (ret == -1) {
        perror("send");
        exit(-1);
    }
    printf("发送成功\n");
}



// 接收水泵信息
WP recv_water_pump(int sock)
{
    int ret = 0;
    WP data;
    memset(&data,0,sizeof(data));
    ret = recv(sock, &data,sizeof(data), 0);
    if (ret == -1) {
        perror("recv");
        exit(-1);
    }
    return data;
}




